package com.cyt.${module?replace('/','.')}.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.${module?replace('/', '.')}.dao.${entity}Dao;
import com.cyt.${module?replace('/', '.')}.model.po.${entity};
import com.cyt.${module?replace('/', '.')}.model.vo.${entity}Vo;
import com.cyt.${module?replace('/', '.')}.service.${entity}Service;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * ${tableComment}Service层实现类
 *
 * @author ${author}
 * @date ${.now}
 */
@Service("${entity?uncap_first}Service")
public class ${entity}ServiceImpl extends BaseServiceImpl<${entity}> implements ${entity}Service {

    @Resource
    private ${entity}Dao ${entity?uncap_first}Dao;

    @Autowired
    public void setBaseDao(${entity}Dao ${entity?uncap_first}Dao) {
        this.baseDao = ${entity?uncap_first}Dao;
    }

    /**
     * 分页查询
     *
     * @param ${entity?uncap_first}Vo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author ${author}
     * @date ${.now}
     */
    @Override
    public ReturnResult findPage(${entity}Vo ${entity?uncap_first}Vo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        Example example = new Example(${entity}.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        List<${entity}> list = ${entity?uncap_first}Dao.selectByExample(example);
        PageInfo<${entity}> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 分页查询
     *
     * @param ${entity?uncap_first}Vo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author ${author}
     * @date ${.now}
     */
    @Override
    public ReturnResult findVoPage(${entity}Vo ${entity?uncap_first}Vo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<${entity}Vo> list = ${entity?uncap_first}Dao.selectVoList(${entity?uncap_first}Vo);
        PageInfo<${entity}Vo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * list查询
     *
     * @param ${entity?uncap_first}Vo 接受参数对象
     * @return List<${entity}>
     * @author ${author}
     * @date ${.now}
     */
    @Override
    public List<${entity}> findList(${entity}Vo ${entity?uncap_first}Vo) {
        Example example = new Example(${entity}.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return ${entity?uncap_first}Dao.selectByExample(example);
    }

    /**
     * list查询
     *
     * @param ${entity?uncap_first}Vo 接受参数对象
     * @return List<${entity}>
     * @author ${author}
     * @date ${.now}
     */
    @Override
    public List<${entity}Vo> findVoList(${entity}Vo ${entity?uncap_first}Vo) {
        return ${entity?uncap_first}Dao.selectVoList(${entity?uncap_first}Vo);
    }

    /**
     * 单个对象查询
     *
     * @param ${entity?uncap_first}Vo 接受参数对象
     * @return ${entity}
     * @author ${author}
     * @date ${.now}
     */
    @Override
    public ${entity} findOne(${entity}Vo ${entity?uncap_first}Vo) {
        ${entity} ${entity?uncap_first} = new ${entity}();
        if (StringUtils.isBlank(${entity?uncap_first}Vo.getId())) {
            ${entity?uncap_first}.setId(${entity?uncap_first}Vo.getId());
        }
        return ${entity?uncap_first}Dao.selectOne(${entity?uncap_first});
    }

    /**
     * 单个对象查询
     *
     * @param ${entity?uncap_first}Vo 接受参数对象
     * @return ${entity}
     * @author ${author}
     * @date ${.now}
     */
    @Override
    public ${entity}Vo findVoOne(${entity}Vo ${entity?uncap_first}Vo) {
        return ${entity?uncap_first}Dao.selectVoOne(${entity?uncap_first}Vo);
    }

    /**
     * 添加或更新
     *
     * @param ${entity?uncap_first}Vo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author ${author}
     * @date ${.now}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(${entity}Vo ${entity?uncap_first}Vo, SysUser sysUser) {
        if (StringUtils.isBlank(${entity?uncap_first}Vo.getId())) {
            ${entity} ${entity?uncap_first} = new ${entity}(sysUser);
<#list table.columns as column>
    <#if !column.pk && column.columnName!="RemoveTag" && column.columnName!="CreateUser" && column.columnName!="CreateTime" && column.columnName!="UpdateTime" && column.columnName!="UpdateUser">
        <#if column.javaType == "Integer">
            ${entity?uncap_first}.set${column.columnName}(Integer.parseInt(${entity?uncap_first}Vo.get${column.columnName}()));
        <#elseif column.javaType == "Double">
            ${entity?uncap_first}.set${column.columnName}(Double.parseDouble(${entity?uncap_first}Vo.get${column.columnName}()));
        <#elseif column.javaType == "Float">
            ${entity?uncap_first}.set${column.columnName}(Float.parseFloat(${entity?uncap_first}Vo.get${column.columnName}()));
        <#elseif column.javaType == "Date">
            ${entity?uncap_first}.set${column.columnName}(DateUtil.parseDateTime(${entity?uncap_first}Vo.get${column.columnName}()));
        <#elseif column.javaType == "String">
            ${entity?uncap_first}.set${column.columnName}(${entity?uncap_first}Vo.get${column.columnName}());
        </#if>
    </#if>
</#list>
            ${entity?uncap_first}Dao.insert(${entity?uncap_first});
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            ${entity} ${entity?uncap_first} = ${entity?uncap_first}Dao.selectByPrimaryKey(${entity?uncap_first}Vo.getId());
<#list table.columns as column>
    <#if !column.pk && column.columnName!="RemoveTag" && column.columnName!="CreateUser" && column.columnName!="CreateTime" && column.columnName!="UpdateTime" && column.columnName!="UpdateUser">
        <#if column.javaType == "Integer">
            ${entity?uncap_first}.set${column.columnName}(Integer.parseInt(${entity?uncap_first}Vo.get${column.columnName}()));
        <#elseif column.javaType == "Double">
            ${entity?uncap_first}.set${column.columnName}(Double.parseDouble(${entity?uncap_first}Vo.get${column.columnName}()));
        <#elseif column.javaType == "Float">
            ${entity?uncap_first}.set${column.columnName}(Float.parseFloat(${entity?uncap_first}Vo.get${column.columnName}()));
        <#elseif column.javaType == "Date">
            ${entity?uncap_first}.set${column.columnName}(DateUtil.parseDateTime(${entity?uncap_first}Vo.get${column.columnName}()));
        <#elseif column.javaType == "String">
            ${entity?uncap_first}.set${column.columnName}(${entity?uncap_first}Vo.get${column.columnName}());
        </#if>
    </#if>
</#list>
            ${entity?uncap_first}.setUpdateTime(new Date());
            ${entity?uncap_first}.setUpdateUser(sysUser.getId());
            ${entity?uncap_first}Dao.updateByPrimaryKey(${entity?uncap_first});
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param ${entity?uncap_first}Vo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author ${author}
     * @date ${.now}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(${entity}Vo ${entity?uncap_first}Vo, SysUser sysUser) {
        //修改字段
        ${entity} ${entity?uncap_first} = new ${entity}();
        ${entity?uncap_first}.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        ${entity?uncap_first}.setUpdateUser(sysUser.getId());
        ${entity?uncap_first}.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(${entity}.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(${entity?uncap_first}Vo.getId())) {
            criteria.andIn("id", Arrays.asList(${entity?uncap_first}Vo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        ${entity?uncap_first}Dao.updateByExampleSelective(${entity?uncap_first}, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param ${entity?uncap_first}Vo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author ${author}
     * @date ${.now}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(${entity}Vo ${entity?uncap_first}Vo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(${entity}.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(${entity?uncap_first}Vo.getId())) {
            criteria.andIn("id", Arrays.asList(${entity?uncap_first}Vo.getId().split(",")));
        }
        ${entity?uncap_first}Dao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}